package com.xfactor.poker.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class MsgSender {

	static String HOST_IP = "127.0.0.1";
	static int PORT = 8888;
	public static void main(String[] args) {
		MsgSender msgSender = new MsgSender();
		msgSender.run(HOST_IP, PORT);
	}

	private void run(String ip, int port) {
		Socket socket = null;
		OutputStream outputStreamToSend = null;
		BufferedReader bufferedReaderFromConsole = null;
		try {
			socket = new Socket(ip, port);
			outputStreamToSend = socket.getOutputStream();
			bufferedReaderFromConsole = new BufferedReader(new InputStreamReader(System.in));
			String msg = null;
			while ((msg = bufferedReaderFromConsole.readLine()) != null) {
//				System.out.println(msg);
				outputStreamToSend.write(msg.getBytes());
				outputStreamToSend.flush();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				if (socket != null) {
					socket.close();
				}
				if(outputStreamToSend != null){
					outputStreamToSend.close();
				}
				if( bufferedReaderFromConsole != null){
					bufferedReaderFromConsole.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}
